﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using MyHealthByDesign.Domain.Panels;

namespace MyHealthByDesign.Domain
{
    /// <summary>
    /// Description:
    /// Question representation for a question on the website
    /// 
    /// Author: Mark Servais
    /// Date Created: 10/23/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// 10/25/2011 Mark Servais - Added question retrieval logic.
    /// 10/28/2011 Mark Servais - Make bug fix to retrieval value in Master Page child.
    /// 11/03/2011 Mark Servais - Added logic to set field values from database. 
    /// 11/19/2011 Mark Servais - Added addition property for question processing
    /// 12/03/2011 Mark Servais - Added active flag to class
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public class Question
    {
        #region variables
        
        #endregion

        #region Properties

        public int Id { get; set; }
        public int Category { get; set; }
        public int SortOrder { get; set; }
        public string DisplayType { get; set; }
        public string Name { get; set; }
        public string ContentLabel { get; set; }
        public string FieldCSS { get; set; }
        public string FieldSuffix { get; set; }
        public string FieldHelpText { get; set; }
        public string FieldHelpURL { get; set; }
        public bool FieldActive { get; set; }
        public List<QuestionOption> FieldOptions { get; set; }
        public List<QuestionProcessing> Processing { get; set; }

        //non database created properties
        public List<HtmlAttribute> Attributes { get; set; }

        #endregion


        #region Methods

        /// <summary>
        /// DisplayQuestion - calls the IQuestionPanel routine for the DisplayType related class
        /// to create an ASP.NET Panel to place on a page to render the question.
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Panel representation the question
        /// </summary>
        /// <returns></returns>
        public Panel DisplayQuestion()
        {
            return DetermineQuestionPanel().GenerateQuestionDisplay(this);
        }

        /// <summary>
        /// RetrieveQuestionValues - calls the IQuestionPanel routine for the DisplayType related class
        /// to retrieve the entered value for this questions when rendered to a page.
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Dictionary collection of questions and answers by field
        /// </summary>
        /// <param name="contentPlaceHolder"></param>
        /// <returns></returns>
        public Dictionary<Question, NameValueCollection> RetrieveQuestionValues(ContentPlaceHolder contentPlaceHolder)
        {
            return DetermineQuestionPanel().RetrieveQuestionValue(this, contentPlaceHolder);
        }

        /// <summary>
        /// SetInitialQuestionAnswer - calls the IQuestionPanel routine for the DisplayType related class
        /// to retrieve the databae value for this question and populate the entry field when rendered to a page.
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Entry field will have the value equal to that in the database
        /// </summary>
        /// <param name="contentPlaceHolder"></param>
        /// <returns></returns>
        public void SetInitialQuestionAnswer(NameValueCollection answers, ContentPlaceHolder contentPlaceHolder)
        {
            DetermineQuestionPanel().SetInitialQuestionValue(this, answers, contentPlaceHolder);
        }

        /// <summary>
        /// GenerateAdminPanels - calls the IQuestionPanel routine for the building the panels that make up the
        /// admnistration form for maintenance
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// List of panels will be returned
        /// </summary>
        /// <param name="panels"></param>
        /// <returns></returns>
        public List<System.Web.UI.WebControls.Panel> GenerateAdminPanels(List<System.Web.UI.WebControls.Panel> panels)
        {
            return DetermineQuestionPanel().GenerateAdminForm(this, panels);
        }

        /// <summary>
        /// ProcessAdminPanels - calls the IQuestionPanel routine for the processing the panels that make up the
        /// admnistration form for maintenance
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// data will be submitted to the database
        /// </summary>
        /// <param name="panels"></param>
        /// <returns></returns>
        public void ProcessAdminPanels(List<System.Web.UI.WebControls.Panel> panels)
        {
            DetermineQuestionPanel().ProcessAdminForm(this, panels);
        }

        
        /// <summary>
        /// DetermineQuestionPanel - shared method to determine Type and create appropriate class type that implements IQuestinoPanel.
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// brings back object that implements IQuestionPanel for processing
        /// </summary>
        /// <returns></returns>
        private IQuestionPanel DetermineQuestionPanel()
        {
            Type typField = typeof(IQuestionPanel).Assembly.GetType(DisplayType);
            IQuestionPanel iqp = (IQuestionPanel)Activator.CreateInstance(typField);

            return iqp;
        }

        #endregion
    }
}